package com.xomily.devops.service.impl;

import java.util.List;

import com.xomily.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xomily.devops.mapper.DevDiscMapper;
import com.xomily.devops.mapper.DevHostMapper;
import com.xomily.devops.mapper.DevNetMapper;
import com.xomily.devops.domain.DevDisc;
import com.xomily.devops.domain.DevHost;
import com.xomily.devops.domain.DevNet;
import com.xomily.devops.service.IDevHostService;

/**
 * 主机信息Service业务层处理
 * 
 * @author xomily
 * @date 2021-11-29
 */
@Service
public class DevHostServiceImpl implements IDevHostService 
{
    @Autowired
    private DevHostMapper devHostMapper;
    @Autowired
    private DevDiscMapper devDiscMapper;
    @Autowired
    private DevNetMapper devNetMapper;
    /**
     * 查询主机信息
     * 
     * @param id 主机信息主键
     * @return 主机信息
     */
    @Override
    public DevHost selectDevHostById(Long id)
    {
        return devHostMapper.selectDevHostById(id);
    }
    
    /**
     * 查询主机详情
     * 
     * @param id 主机信息主键
     * @return 主机信息
     */
    public DevHost selectDevHostDetailById(Long id)
    {
    	DevHost host = devHostMapper.selectDevHostById(id);
    	if (host != null) {
            List<DevDisc> discs = devDiscMapper.selectDevDiscByIp(host.getIp());
            List<DevNet> nets = devNetMapper.selectDevNetByIp(host.getIp());
    		host.setDiscs(discs);
    		host.setNets(nets);
    	}
    	return host;
    }
    
    
    /**
     * 查询主机信息
     * 
     * @param ip 主机信息主键
     * @return 主机信息
     */
    public DevHost selectDevHostByIp(String ip)
    {
    	return devHostMapper.selectDevHostByIp(ip);
    }
    
    /**
     * 查询主机信息列表
     * 
     * @param devHost 主机信息
     * @return 主机信息
     */
    @Override
    public List<DevHost> selectDevHostList(DevHost devHost)
    {   	
        return devHostMapper.selectDevHostList(devHost);
    }

    
    /**
     * 新增主机信息
     * 
     * @param devHost 主机信息
     * @return 结果
     */
    @Override
    public int insertDevHost(DevHost devHost)
    {
            devHost.setCreateTime(DateUtils.getNowDate());
            devHost.setUpdateTime(DateUtils.getNowDate());
            return devHostMapper.insertDevHost(devHost);

    }

    /**
     * 更新主机信息
     * 
     * @param devHost 主机信息
     * @return 结果
     */
    public int updateAgentHost(DevHost devHost)
    {
        DevHost hostQuery = devHostMapper.selectDevHostByIp(devHost.getIp());
        if (hostQuery != null ) {
        	devHost.setId(hostQuery.getId());
        	devHost.setUpdateTime(DateUtils.getNowDate());
            return devHostMapper.updateDevHostByInfo(devHost);
        }else {
            return -1;
        }
    }

    
    /**
     * 修改主机信息
     * 
     * @param devHost 主机信息
     * @return 结果
     */
    @Override
    public int updateDevHost(DevHost devHost)
    {
        return devHostMapper.updateDevHost(devHost);
    }

    /**
     * 修改状态
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateDevHostStatus(DevHost devHost)
    {
    	if (devHost.getStatus()=="0") {
    		devHost.setRunStatus("0");
    	}
        return devHostMapper.updateDevHost(devHost);
    }

    /**
     * 批量删除主机信息
     * 
     * @param ids 需要删除的主机信息主键
     * @return 结果
     */
    @Override
    public int deleteDevHostByIds(Long[] ids)
    {
        return devHostMapper.deleteDevHostByIds(ids);
    }

    /**
     * 删除主机信息信息
     * 
     * @param id 主机信息主键
     * @return 结果
     */
    @Override
    public int deleteDevHostById(Long id)
    {
        return devHostMapper.deleteDevHostById(id);
    }
}
